<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class login extends CI_Controller {
	function __construct(){
		parent::__construct();
		$this->host=base_url().'backend/';
		$this->load->model('mdata');
		$this->sesina	= unserialize(base64_decode($this->session->userdata('ses_data')));
	}
	
	public function index()
	{
		if($this->sesina){
			header("Location: " .base_url().'backend');
		}
		else{
			$data["pesan"]="";
			$data["user"]="";
			$data["pass"]="";
			$this->load->view('login',$data);
		}
	}
	
	function login_na($p1=""){ 
		$user = $this->input->post('user',true);
		$pass = $this->input->post('pass',true);
		$this->load->library('encrypt');
		
		if(!$user or !$pass){
			$data["pesan"]="Tolong Isi Username Dan Password!";
			$data["user"]="";
			$data["pass"]="";
			if($p1!='front'){
				$this->load->view("login",$data);
				return;
			}
			
			else{
				echo 3;exit;
			}
		};
		
		$data = $this->mdata->get_data('tbl_user',$user); 
		if($data){// && $pass == $this->encrypt->decode($data[0]['password'])){
            $this->session->set_userdata('ses_data', base64_encode(serialize($data[0])));	
			if($p1!='front'){
            	header("Location: " . $this->host);
			}
			else{
				echo 1;exit;
			}
			
		} else {
			$data["pesan"]="Username Dan Password Salah!!";
			$data["user"]=$user;
			$data["pass"]=$pass;
			if($p1!='front'){
				$this->load->view("login",$data);
			}
			else{echo 2;exit;}
		}	
		
	}
	
	function logout_na(){
		$this->session->unset_userdata('ses_data', 'limit');
		$this->session->sess_destroy();
		header("Location: " .base_url());
	}
	
		
	function logout_na2(){
		$this->session->unset_userdata('ses_data', 'limit');
		$this->session->sess_destroy();
		header("Location: " .base_url()."login/");
	}
	
	
}